﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;

using AKMII.DMRA.Common;
using AKMII.DMRA.Business.Management;
using AKMII.DMRA.Business.Service;
using AKMII.DMRA.Business.Rule;

namespace AKMII.DMRA.Web.AdminPages
{
    public partial class ManageMonthSetting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSelectedMonthData();
            }
        }

        private void BindSelectedMonthData()
        {
            SettingManager sm = new SettingManager();
            MonthSetting monthset = sm.GetMonthSetting(Convert.ToInt32(this.ddlMonth.SelectedValue));
            if (monthset != null)
            {
                this.txtCutOffEnd.Text = monthset.CutOffEndDate.ToString("yyyy-MM-dd");
                this.txtCutOffStart.Text = monthset.CutOffStartDate.ToString("yyyy-MM-dd");
                this.txtEndMonth.Text = monthset.EndDate.ToString("yyyy-MM-dd");
                this.txtStartMonth.Text = monthset.StartDate.ToString("yyyy-MM-dd");
                this.txtUploadDate.Text = monthset.UploadDate.ToString("yyyy-MM-dd");
            }
        }

        protected void btnCommit_Click(object sender, EventArgs e)
        {
            SettingManager sm = new SettingManager();
            MonthSetting ms = new MonthSetting();
            ms.UploadDate = Convert.ToDateTime(this.txtUploadDate.Text);
            ms.StartDate = Convert.ToDateTime(this.txtStartMonth.Text);
            ms.EndDate = Convert.ToDateTime(this.txtEndMonth.Text);
            ms.CutOffStartDate = Convert.ToDateTime(this.txtCutOffStart.Text);
            ms.CutOffEndDate = Convert.ToDateTime(this.txtCutOffEnd.Text);
            ms.MonthName = Convert.ToInt32(this.ddlMonth.SelectedValue);
            MonthSetting oldSetting = sm.GetMonthSetting(Convert.ToInt32(this.ddlMonth.SelectedValue));

            if (sm.UpdateMonthSetting(ms))
            {
                SystemConfiguration.MonthSettings = sm.GetMonthSettings();
                Thread thread = new Thread(SendMail);
                thread.Start(GetChangeSettings(oldSetting, sm.GetMonthSetting(Convert.ToInt32(this.ddlMonth.SelectedValue))));

                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), 
                    "updated", "<script type='text/javascript'>alert('You have saved setting successfully! ');</script>");
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), 
                    "failed", "<script type='text/javascript'>alert('You have failed ! ');</script>");
            }
            
            BindSelectedMonthData();
        }

        private void SendMail(object content)
        {
            if (content != null && content.ToString() != string.Empty)
            {
                Mail eMail = new Mail();
                eMail.Subject = Constants.MailSubjectMonthSetting;
                eMail.Content = string.Format(Constants.MailContentMonthSetting,
                    DateTime.Now.Date.ToString("MM/dd/yyyy") + "@" + DateTime.Now.ToShortTimeString(), User.Identity.Name, content);
                AccountManager am = new AccountManager();
                eMail.Recipients = am.GetMailAddress(UserType.Administrator);
                SmtpMailService smtp = new SmtpMailService(SystemConfiguration.CommonSetting.SmtpServer,
                    SystemConfiguration.CommonSetting.SystemMailAddress, SystemConfiguration.CommonSetting.MailPassword);
                smtp.Send(eMail);
            }
        }

        private string GetChangeSettings(MonthSetting oldSetting, MonthSetting newSetting)
        {
            StringBuilder changes = new StringBuilder(string.Empty);
            string month = ddlMonth.Text;

            changes.Append((newSetting.CutOffEndDate == oldSetting.CutOffEndDate) ? string.Empty : month + " Cut Off End Date:\nForm: " + oldSetting.CutOffEndDate.ToString("MM/dd/yyyy") + "\nTo: " + newSetting.CutOffEndDate.ToString("MM/dd/yyyy") + "\n");
            changes.Append((newSetting.CutOffStartDate == oldSetting.CutOffStartDate) ? string.Empty : month + " Cut Off Start Date:\nForm: " + oldSetting.CutOffStartDate.ToString("MM/dd/yyyy") + "\nTo: " + newSetting.CutOffStartDate.ToString("MM/dd/yyyy") + "\n");
            changes.Append((newSetting.StartDate == oldSetting.StartDate) ? string.Empty : month + " Start Date Of Month:\nForm: " + oldSetting.StartDate.ToString("MM/dd/yyyy") + "\nTo:" + newSetting.StartDate.ToString("MM/dd/yyyy") + "\n");
            changes.Append((newSetting.EndDate == oldSetting.EndDate) ? string.Empty : month + " End Date Of Month:\nForm: " + oldSetting.EndDate.ToString("MM/dd/yyyy") + "\nTo: " + newSetting.EndDate.ToString("MM/dd/yyyy") + "\n");
            changes.Append((newSetting.UploadDate == oldSetting.UploadDate) ? string.Empty : month + " UpLoad Date:\nForm: " + oldSetting.UploadDate.ToString("MM/dd/yyyy") + "\nTo: " + newSetting.UploadDate.ToString("MM/dd/yyyy") + "\n");
            
            return changes.ToString();
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindSelectedMonthData();
        }
    }
}
